<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    include "../../include/dbFunctions.php";
    $vid=$_GET['id'];
    $eid=$_SESSION['eid'];
    $query="UPDATE trainer
            SET trainer_status = 'accept'
            WHERE volunteer_id = $vid AND event_id = $eid";
    $result = executeQuery($query);
    if ($result!=0) {
        $_SESSION['mes']="The record has been updated successfully";
        //update event status
        $query2= "SELECT volunteer_id FROM trainer WHERE event_id = $eid AND trainer_status = 'confirmed'";
        $trainer_array = executeSelectQuery($query2);
        $query3= "SELECT (class_id) FROM class WHERE event_id = $vid ";
        $class = executeSelectQuery($query3);
        $no_ready = count($trainer_array);
        $trainer_required = count($class)*2;
        if ($no_ready!=$trainer_required) {
            $query = "UPDATE event
            SET event_status = 'pending'
            WHERE event_id = $eid";
            $result = executeQuery($query);
        } 
        header('Location: ' . $_SERVER['HTTP_REFERER']);

    } else {
        $_SESSION['mes']="Error! Please try again";
        header('Location: ' . $_SERVER['HTTP_REFERER']);
    }
    ?>
    <?php
}else {
    header("Location: ../include/login.html");
}
?>